import Terminal as Terminal
import threading
import time
import urllib.request
import json

longitude = [104.00447562197,
             103.996011493617,
             103.992009905651,
             103.989957488716,
             103.975351654065,
             103.971203048222,
             103.966804132815,
             103.964851139115,
             103.958205713664,
             103.947320574084,
             103.938326212545,
             103.930101885631,
             103.918400400054,
             103.914524156626,
             103.908280556564,
             103.907123068268,
             103.908593522769,
             103.910171012428,
             103.911744839171,
             103.915126011162,
             103.918773973611,
             103.921327383101,
             103.9238801603,
             103.932098507976,
             103.935862849043,
             103.940285216717,
             103.945346961695,
             103.947677310371,
             103.948774914902,
             103.951787335671,
             103.95950504772,
             103.961658224195,
             103.96390482923,
             103.965735345131,
             103.968313270891,
             103.967599013086,
             103.968935273057,
             103.971589859382,
             103.97851011612,
             103.981838755872,
             103.984689925464,
             103.988410319833,
             103.995819443063,
             103.999922633367,
             104.004812721577,
             104.009274192821,
             104.013206657815,
             104.016589040921,
             104.022542517717,
             104.024947027268,
             104.025874289717]
latitude = [
    30.5653145743436,
    30.5631882687498,
    30.562365996165,
    30.5621068077197,
    30.561219729966,
    30.5606303873525,
    30.5595396553233,
    30.5588685475683,
    30.5564079510906,
    30.5524779830158,
    30.5490775031886,
    30.5458473277795,
    30.5414431834554,
    30.5398404693015,
    30.5369394176011,
    30.5353857142848,
    30.5330152113058,
    30.5275038417217,
    30.5271522291613,
    30.5254761479918,
    30.5225393671511,
    30.5186550176841,
    30.5155342070846,
    30.5089630935543,
    30.5062303245535,
    30.5039263324476,
    30.5022214636546,
    30.5017086250675,
    30.5015276743834,
    30.5012015779445,
    30.5007982819539,
    30.5005522521139,
    30.4999654931126,
    30.4996799280507,
    30.4986781590606,
    30.497976133211,
    30.4998450947584,
    30.5027558758725,
    30.5100238649328,
    30.5148847312383,
    30.5197104073642,
    30.524186668268,
    30.5317891489321,
    30.5358128171008,
    30.5390566562864,
    30.542205137938,
    30.5460591785786,
    30.5507857757654,
    30.5605383980302,
    30.5646796701572,
    30.5665429315073

]
speed = [
    0,
    41,
    43,
    7,
    35,
    54,
    42,
    20,
    56,
    54,
    26,
    68,
    47,
    22,
    43,
    28,
    36,
    5,
    34,
    52,
    59,
    56,
    46,
    57,
    57,
    63,
    59,
    0,
    25,
    31,
    18,
    31,
    10,
    28,
    30,
    22,
    39,
    54,
    75,
    72,
    71,
    68,
    68,
    72,
    67,
    65,
    72,
    75,
    65,
    55,
    16

]


def rest(apiname, values={"vins": ["SXQC0512"]}):
    opener = urllib.request.build_opener()
    request = urllib.request.Request('http://172.16.6.148:8081/%s' % apiname,
                                     json.dumps(values).encode(encoding='utf8'))
    response = opener.open(request)
    print(response.read().decode("utf8"))


def auto_run():
    time.sleep(5)
    vin = 'LZGTT3W45YY000001'
    data = {"vins": [vin], "cmd": 0x00}
    apiname = "VehicleControlMode"
    rest(apiname, data)
    time.sleep(5)

    data = {"vins": [vin], "cmd": 0x20}
    rest(apiname, data)
    time.sleep(5)

    apiname = "VehicleNavigation"
    data = {"vins": [vin], "route_id": 1, "items": []}
    for i in range(len(longitude)):
        data["items"].append({"id": i,
                              "radius": 0,
                              "latitude": int(latitude[i] * 1000000),
                              "longitude": int(longitude[i] * 1000000),
                              "width": 500,
                              "speed": speed[i]})
    rest(apiname, data)


def start_terminal():
    #t = Terminal.Terminal(host='172.16.6.148', port=8084, vin='LZGTT3W45YY000001', iccid='38393836303331363235')
    t = Terminal.Terminal(host='172.16.6.78', port=7160, vin='LZGK7S418JT000002', iccid='8986011880101805798v')
    t.start()


if __name__ == '__main__':
    terminal_thread = threading.Thread(target=start_terminal, args=())
    terminal_thread.start()
    # auto_run_thread = threading.Thread(target=auto_run, args=())
    # auto_run_thread.start()
